package au.com.iglootech.winerymap;

import java.io.Serializable;

import au.com.iglootech.winerymap.entity.WMUser;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;

/**
 * 
 * @author Nicholas Zhu
 * 
 */
public class UserSessionData implements TransactionListener, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 639425014049471973L;

	// private ResourceBundle bundle;
	// private Locale locale; // Current locale
	private WMUser userData; // Trivial data model for the user

	private final Application app; // For distinguishing between apps

	private static ThreadLocal<UserSessionData> instance = new ThreadLocal<UserSessionData>();

	/**
	 * 
	 * @param app
	 *            Application
	 */
	public UserSessionData(final Application app) {
		this.app = app;

		// It's usable from now on in the current request
		instance.set(this);
	}

	/**
	 * 
	 * @param application
	 *            application
	 */
	public void transactionEnd(final Application application) {
		if (this.app == application) {
			instance.set(null);
		}
	}

	/**
	 * 
	 * @param application
	 *            application
	 */
	public void transactionStart(final Application application) {
		if (this.app == application) {
			instance.set(this);
		}
	}

	@Override
	public void transactionStart(final Application application, final Object transactionData) {
		// Set this data instance of this application
		// as the one active in the current thread.
		if (this.app == application) {
			instance.set(this);
		}
	}

	@Override
	public void transactionEnd(final Application application, final Object transactionData) {
		// Clear the reference to avoid potential problems
		if (this.app == application) {
			instance.set(null);
		}
	}

	// public static void initLocale(Locale locale,
	// String bundleName) {
	// instance.get().locale = locale;
	// instance.get().bundle =
	// ResourceBundle.getBundle(bundleName, locale);
	// }
	//
	// public static Locale getLocale() {
	// return instance.get().locale;
	// }
	//
	// public static String getMessage(String msgId) {
	// return instance.get().bundle.getString(msgId);
	// }

	/**
	 * 
	 * @return WMUser
	 */
	public static WMUser getUserData() {
		if (instance.get() != null) {
			return instance.get().userData;
		} else {
			return null;
		}
	}

	/**
	 * 
	 * @param userData
	 *            user
	 */
	public static void setUserData(final WMUser userData) {
		if (instance.get() != null) {
			instance.get().userData = userData;
		}
	}

	/**
	 * 
	 * @return Application
	 */
	public Application getApp() {
		return app;
	}

	/**
	 * @return the instance
	 */
	public static ThreadLocal<UserSessionData> getInstance() {
		return instance;
	}

	/**
	 * @param instance
	 *            the instance to set
	 */
	public static void setInstance(final ThreadLocal<UserSessionData> instance) {
		UserSessionData.instance = instance;
	}

}